/**
 * @author LHJ
 */

/**
 * @function 核对注册信息
 * @returns {boolean}
 */

var regist_email_ok;
var regist_code_ok;

function checkRegistInfo() {
	if (typeof(regist_email_ok) == "undefined") {
		checkUserRegistEmail();
	}
	
	if (typeof(regist_code_ok) == "undefined") {
		doCheck();
	}
	
	if( !checkUserRegistNickname() || !regist_email_ok
			|| !checkUserRegistPassword() || !checkUserReRegistPassword()
			|| !checkUserRegistSex() || !checkUserRegistBirthday() || !regist_code_ok  
			) {

		enableSubmitButton();
		return false;
	}
	disableSubmitButton();
	return true;
}

function disableSubmitButton() {
	$("#regist-submit-button").css("display", "none");
	$("#loadingImg").css("display", "block");
}
function enableSubmitButton() {
	$("#regist-submit-button").css("display", "block");
	$("#loadingImg").css("display", "none");
}

/**
 * @function 核对注册邮箱
 * @returns {Boolean}
 */
function checkUserRegistEmail() {
	var userRegistEmail = $('#userRegistEmail').val();

	if(!validateInput(userRegistEmail,3,30,"E")) {
		$('#userRegistEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERID_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	$('#userRegistEmailError').html('<img src="'+WEBROOT+'/resource/images/pages/loading.gif" class="email-checkloading-img"/>');
	$.ajax({
		url: 'ajaxCheckUserRegistEmailExist',
		data: {
			'userRegistEmail':userRegistEmail
		},
		type: 'post',
		async: true,
		dataType: 'json',
		success: function(res) {
			if(res) {
				$('#userRegistEmailError img').css("display", "none");
				$('#userRegistEmailError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
				regist_email_ok = true;
			}else {
				$('#userRegistEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_REGIST_MAIL+'</font>');
				regist_email_ok = false;
			}
		}
	});
	
	return regist_email_ok;
}

/**
 * @function 核对注册昵称
 * @returns {Boolean}
 */
function checkUserRegistNickname() {
	var userRegistNickname = $('#userRegistNickname').val();
	if(isEmpty(userRegistNickname)) {
		$('#userRegistNicknameError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		return false;
	}
	if(!validateInput(userRegistNickname, 2, 10, "0aAC")) {
		$('#userRegistNicknameError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERNICKNAME_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	$('#userRegistNicknameError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册密码
 * @returns {Boolean}
 */
function checkUserRegistPassword() {
	var userRegistPassword = $('#userRegistPassword').val();
	if(isEmpty(userRegistPassword)) {
		$('#userRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		return false;
	}
	if(!validateInput(userRegistPassword, 6, 20, "0aA")) {
		$('#userRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERPASSWORD_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	$('#userRegistPasswordError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册确认密码
 * @returns {Boolean}
 */
function checkUserReRegistPassword() {
	var userRegistPassword = $('#userRegistPassword').val();
	var userReRegistPassword = $('#userReRegistPassword').val();
	
	if(isEmpty(userReRegistPassword)) {
		$('#userReRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		
		return false;
	}
	if(userRegistPassword != userReRegistPassword) {
		$('#userReRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERREPASSWORD_IS_NOT_VALID+'</font>');
		$('#userReRegistPassword').val("");
		return false;
	}
	$('#userReRegistPasswordError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册性别
 * @returns {Boolean}
 */
function checkUserRegistSex() {
	var userRegistSex = $('#userRegistSex').val();
	return true;
}

/**
 * @function 核对注册生日
 * @returns {Boolean}
 */
function checkUserRegistBirthday() {
	var userRegistBirthday = $('#userRegistBirthday').val();
	var regist = /^\d{4}-\d{2}-\d{2}$/;
	if(!(regist.test(userRegistBirthday)))
	{
		$('#userReRegistBirthdayError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_REGIST_BIRTHDAY+'</font>');
		return false;
	}
	$('#userReRegistBirthdayError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}
/*
 * 
 * 验证验证码
 * 
 * */
function doCheck(){
	var userRegistcheck = $('#yancheck').val();
	if(isEmpty(userRegistcheck)){
		$("#docheck").html("<font color='red'>"+ErrorMsgConstants.ERR_CHECK+"</font>");
		regist_code_ok = false;
		return regist_code_ok;
	}
	
	$.ajax({ 
		url: 'ajaxuserRegistcheck',
		data: {
			'userRegistyancheck':userRegistcheck
		},
		
		type: 'post',
		async: true,
		dataType: 'json',
		success: function(res) {
			
			if(res)
				{
				$("#docheck").html("<font color='green'>"+ValidMsgConstants.VALID_INPUT+"</font>");
				regist_code_ok = true;
				}
			else{
				$("#docheck").html("<font color='red'>"+ErrorMsgConstants.ERR_CHECK+"</font>");
				img();
				regist_code_ok = false;
			}
			
		}
	});
	
	return regist_code_ok;
}